<template>
  <div class="container fade-in">
    <div class="header">
      <h2 class="section-title">收款码详情</h2>
      <div class="actions">
        <router-link to="/payment-codes" class="btn secondary">返回列表</router-link>
      </div>
    </div>

    <div class="card slide-up" style="animation-delay: 0.2s">
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>
      <div v-else-if="!code" class="empty-state">
        <div class="icon">❓</div>
        <h3>未找到该收款码</h3>
        <p>请返回列表重新选择</p>
      </div>
      <div v-else class="detail-grid">
        <div class="row"><div class="label">收款码编号</div><div class="value">{{ code.code || '-' }}</div></div>
        <div class="row"><div class="label">名称</div><div class="value">{{ code.codeName || '-' }}</div></div>
        <div class="row"><div class="label">所属门店</div><div class="value">{{ code.storeName || code.storeId || '-' }}</div></div>
        <div class="row"><div class="label">启用状态</div><div class="value">{{ enableText(code.enableStatus) }}</div></div>
        <div class="row"><div class="label">分账协议</div><div class="value">{{ code.dividedAgreementId ? code.dividedAgreementId : '未设置' }}</div></div>
        <div class="row"><div class="label">结算周期</div><div class="value">{{ code.settlementCycle ?? '-' }}</div></div>
        <div class="row"><div class="label">创建时间</div><div class="value">{{ formatDate(code.creationTime) }}</div></div>
        <div class="row"><div class="label">收款码URL</div><div class="value">{{ code.codeUrl || '-' }}</div></div>
        <div class="row"><div class="label">关联成员</div><div class="value">
          <span v-if="code.bindUserName && code.bindUserName.length > 0">{{ code.bindUserName.join('、') }}</span>
          <span v-else class="text-secondary">未关联</span>
        </div></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { PaymentCodeService, type PaymentCodeResponse } from '../services/paymentCode'
import { isSuccess } from '../services/types'

const route = useRoute()
const id = String(route.params.id || '')
const loading = ref(false)
const code = ref<PaymentCodeResponse | null>(null)

function formatDate(v?: string) {
  if (!v) return '-'
  try { return new Date(v).toLocaleString('zh-CN') } catch { return v }
}
function enableText(s?: number) {
  if (s === 1) return '启用'
  if (s === 0) return '禁用'
  return '未知'
}

async function loadDetail() {
  loading.value = true
  try {
    const res = await PaymentCodeService.getPaymentCodeDetail({ id })
    if (isSuccess(res)) {
      code.value = res.data || null
    } else {
      code.value = null
      console.warn('获取收款码详情失败:', res?.message)
    }
  } catch (e) {
    console.error('接口异常:', e)
  } finally {
    loading.value = false
  }
}

onMounted(loadDetail)
</script>

<style scoped>
.detail-grid { display: grid; grid-template-columns: 200px 1fr; gap: 12px; }
.row { display: contents; }
.label { color: var(--text-secondary); }
.value { color: var(--text); font-weight: 500; }
.text-secondary { color: var(--text-secondary); }
.loading-state, .empty-state { text-align: center; padding: var(--space-6); color: var(--text-secondary); }
.loading-spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--primary-600); border-radius: 50%; display:inline-block; animation: spin 0.8s linear infinite; margin-right: 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
</style>

